package com.zyx.juc.cf.my;

import java.util.concurrent.*;

/**
 * @author Yaxi.Zhang
 * @since 2022/6/10 18:30
 */
public class CompletableFutureAllOfDemo2 {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Book book = new Book();

        CompletableFuture<Void> cf1 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(4000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("get name success....");
            book.setName("Java编程宝典");
        });

        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(4000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("get price success....");
            book.setPrice(60.12);
        });

        CompletableFuture.allOf(cf1, cf2).join();
        System.out.println("bookinfo:" + book);
        executor.shutdown();
    }
}
